package com.lineage.data.npc.shop;

import com.lineage.config.ConfigOther;
import com.lineage.config.ConfigRecord;
import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.lock.LogEnchantReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.william.GiveBack;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Npc_GiveBack extends NpcExecutor {
  private static final Log _log = LogFactory.getLog(Npc_GiveBack.class);
  
  public static NpcExecutor get() {
    return new Npc_GiveBack();
  }
  
  public int type() {
    return 3;
  }
  
  public void talk(L1PcInstance pc, L1NpcInstance npc) {
    try {
      String str1 = null;
      int count = 0;
      for (int i = 0; i < GiveBack.savepcid.size(); i++) {
        if (((Integer)GiveBack.savepcid.get(i)).intValue() == pc
          .getId())
          count++; 
      } 
      pc.set_backpage(1);
      String msg0 = " ";
      String msg1 = " ";
      String msg2 = " ";
      String msg3 = " ";
      int count2 = 0;
      for (int j = 0; j < GiveBack.savepcid.size(); j++) {
        if (((Integer)GiveBack.savepcid.get(j)).intValue() == pc
          .getId()) {
          count2++;
          if (pc.get_backpage() == count2) {
            msg0 = GiveBack.savename.get(j);
            break;
          } 
        } 
      } 
      int k = pc.get_backpage();
      int m = count;
      String[] price = ConfigOther.Item_Price.split(",");
      L1Item temper = ItemTable.get().getTemplate(
          Integer.valueOf(price[0]).intValue());
      msg3 = String.valueOf(price[1]) + "个" + temper.getName();
      if (count == 0)
        str1 = "0"; 
      String[] msg = { msg0, str1, ""+m, msg3 };
      if (count > 0)
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "giveback", msg)); 
      if (count == 0)
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "nogiveback", 
              msg)); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {
    if (cmd.equals("accept")) {
      int count = 0;
      for (int i = 0; i < GiveBack.savepcid.size(); i++) {
        if (((Integer)GiveBack.savepcid.get(i)).intValue() == pc
          .getId()) {
          count++;
          if (count == pc.get_backpage()) {
            String[] price = ConfigOther.Item_Price.split(",");
            int itemids = Integer.valueOf(price[0]).intValue();
            int counts = Integer.valueOf(price[1]).intValue();
            if (pc.getInventory().checkItem(itemids, counts)) {
              pc.getInventory().consumeItem(itemids, counts);
              L1ItemInstance olditem = GiveBack.saveweapon
                .get(i);
              int count2 = 0;
              Iterator<Integer> iter = GiveBack.savepcid
                .iterator();
              while (iter.hasNext()) {
                count2++;
                int s = ((Integer)iter.next()).intValue();
                if (s == pc.getId() && 
                  count2 == pc.get_backpage()) {
                  iter.remove();
                  break;
                } 
              } 
              int count3 = 0;
              Iterator<L1ItemInstance> iter2 = GiveBack.saveweapon
                .iterator();
              while (iter2.hasNext()) {
                count3++;
                L1ItemInstance s = iter2
                  .next();
                if (s == olditem && 
                  count3 == pc.get_backpage()) {
                  iter2.remove();
                  break;
                } 
              } 
              int count4 = 0;
              Iterator<String> iter3 = GiveBack.savename
                .iterator();
              while (iter3.hasNext()) {
                count4++;
                String s = iter3.next();
                if (s.equals(olditem.getNumberedViewName(1L)) && 
                  count4 == pc.get_backpage()) {
                  iter3.remove();
                  break;
                } 
              } 
              if (ConfigRecord.LOGGING_BAN_ENCHANT)
                LogEnchantReading.get().resetEnchant(pc, 
                    olditem); 
              pc.getInventory().storeItem(olditem, 1);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV 您的 \\fV " + 
                    olditem.getNumberedViewName(1L) + "\\fV  已赎回。"));
              pc.set_backpage(1);
              pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
            } else {
              L1Item temper = ItemTable.get()
                .getTemplate(itemids);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(String.valueOf(temper.getName()) + 
                    " 不足。"));
              pc.set_backpage(1);
              pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
            } 
          } 
        } 
      } 
    } 
    if (cmd.equals("prev")) {
      String str1 = null;
      int count = 0;
      for (int i = 0; i < GiveBack.savepcid.size(); i++) {
        if (((Integer)GiveBack.savepcid.get(i)).intValue() == pc
          .getId())
          count++; 
      } 
      pc.set_backpage(pc.get_backpage() - 1);
      if (pc.get_backpage() <= 1)
        pc.set_backpage(1); 
      String msg0 = " ";
      String msg1 = " ";
      String msg2 = " ";
      String msg3 = " ";
      int count2 = 0;
      for (int j = 0; j < GiveBack.savepcid.size(); j++) {
        if (((Integer)GiveBack.savepcid.get(j)).intValue() == pc
          .getId()) {
          count2++;
          if (pc.get_backpage() == count2) {
            msg0 = GiveBack.savename.get(j);
            break;
          } 
        } 
      } 
      int k = pc.get_backpage();
      int m = count;
      String[] price = ConfigOther.Item_Price.split(",");
      L1Item temper = ItemTable.get().getTemplate(
          Integer.valueOf(price[0]).intValue());
      msg3 = String.valueOf(price[1]) + "个" + temper.getName();
      if (count == 0)
        str1 = "0"; 
      String[] msg = { msg0, str1, ""+m, msg3 };
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "giveback", msg));
    } 
    if (cmd.equals("next")) {
      String str1 = null;
      int count = 0;
      for (int i = 0; i < GiveBack.savepcid.size(); i++) {
        if (((Integer)GiveBack.savepcid.get(i)).intValue() == pc
          .getId())
          count++; 
      } 
      pc.set_backpage(pc.get_backpage() + 1);
      if (pc.get_backpage() > count)
        pc.set_backpage(count); 
      String msg0 = " ";
      String msg1 = " ";
      String msg2 = " ";
      String msg3 = " ";
      int count2 = 0;
      for (int j = 0; j < GiveBack.savepcid.size(); j++) {
        if (((Integer)GiveBack.savepcid.get(j)).intValue() == pc
          .getId()) {
          count2++;
          if (pc.get_backpage() == count2) {
            msg0 = GiveBack.savename.get(j);
            break;
          } 
        } 
      } 
      int k = pc.get_backpage();
      int m = count;
      String[] price = ConfigOther.Item_Price.split(",");
      L1Item temper = ItemTable.get().getTemplate(
          Integer.valueOf(price[0]).intValue());
      msg3 = String.valueOf(price[1]) + "个" + temper.getName();
      if (count == 0)
        str1 = "0"; 
      String[] msg = { msg0, str1, ""+m, msg3 };
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "giveback", msg));
    } 
  }
}
